package com.apes.enquiry.settle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.Business;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupplierPlatformSettle.CONST_TABLE_NAME)
public class SupplierPlatformSettle extends Model {
    public static final String CONST_TABLE_NAME = "SUPPLIER_PLATFORM_SETTLE";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商对账单", prefix = "PJ%(yy)%(month)", padding = 5, useDateRange=true)
    @Comment("编码")
    private String id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("平台公司编码")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "supplier_id", referencedColumnName = "id")
    @Comment("供应商编码")
    private PartyRole supplier;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门")
    private Dept dept;

//    @ManyToOne(fetch = FetchType.EAGER)
//    @JoinColumn(name = "cooperation_id", referencedColumnName = "id")
//    @Comment("合作方式")
//    private Cooperation cooperation; //合作方式
//
//    @ManyToOne(fetch = FetchType.EAGER)
//    @JoinColumn(name = "reconcile_uid", referencedColumnName = "id")
//    @Comment("对账人")
//    private PartyRole reconcileUid;
//
//    @ManyToOne(fetch = FetchType.EAGER)
//    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
//    @Comment("审批人")
//    private PartyRole approveUid;

    @Column(name = "payment_month")
    @Comment("货款月份")
    private String paymentMonth;

    @Column(name = "date_approve")
    @Comment("审批日期")
    private Date dateApprove;

    @Column(name = "date_payment")
    @Comment("确认日期")
    private Date datePayment;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "reconcile_begin_date")
    @Comment("开始对账日期")
    private Date reconcileBeginDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "reconcile_end_date")
    @Comment("结束对账日期")
    private Date reconcileEndDate;

    @Column(name = "loan_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("贷款金额")
    private double loanAmt = 0;

    @Column(name = "rebate_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("扣除返利")
    private double rebateAmt = 0;

    @Column(name = "cost_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("扣除费用")
    private double costAmt = 0;

    @Column(name = "payable_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("结算金额")
    private double payableAmt = 0;

    @Column (name="inventory_amount" , nullable=false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("库存余额")
    private double inventoryAmount = 0;

    @Column (name="prepaid_balance" , nullable=false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("预付余额")
    private double prepaidBalance = 0;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;

    @Column(name = "reason", length = 250)
    @Comment("退回原因")
    private String reason;

    @States(name = "结算状态", values = {
            @State(id = "sent", name = "结算申请"),
            @State(id = "approve", name = "已审核"),
            @State(id = "payment", name = "已付款"),
            @State(id = "section", name = "部分到票"),
            @State(id = "already", name = "已到票")})
    @Column(name = "state", length = 10, nullable = false)
    private String state;

    @Column(name = "discount_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("合计折扣金额")
    private double discountAmount;

    @States(name = "合同类型", values = {
            @State(id = "contract", name = "采购合同"),
            @State(id = "protocol", name = "平台协议")})
    @Column(name = "type", length = 10)
    private String type;

//    public double getDiscountAmount() {
//        return this.platformSettleItems.stream().mapToDouble(PlatformSettleItem::getDiscountAmount).sum();
//    }
//
//    @OneToMany(mappedBy = "platformSettle", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, orphanRemoval = true)
//    private List<PlatformSettleItem> platformSettleItems = new ArrayList<>();
//
//    public List<PlatformSettleItem> getPlatformSettleItems() {
//        if (platformSettleItems == null) {
//            platformSettleItems = new ArrayList<>();
//        }
//        for (PlatformSettleItem item : platformSettleItems) {
//            item.setPlatformSettle(this);
//        }
//        return platformSettleItems;
//
//    }
//
//    @OneToMany(mappedBy = "platformSettle", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, orphanRemoval = true)
//    private List<PlatformSettleItemSummary> platformSettleItemSummaryList = new ArrayList<>();
//
//    public List<PlatformSettleItemSummary> getPlatformSettleItemSummaryList() {
//        if (platformSettleItemSummaryList == null) {
//            platformSettleItemSummaryList = new ArrayList<>();
//        }
//        for (PlatformSettleItemSummary item : platformSettleItemSummaryList) {
//            item.setPlatformSettle(this);
//        }
//        return platformSettleItemSummaryList;
//
//    }
//
//    @OneToMany(mappedBy = "platformSettle", cascade = {CascadeType.ALL}, orphanRemoval = true)
//    private List<PlatformSettleRebate> platformSettleRebates = new ArrayList<>();
//    public List<PlatformSettleRebate> getPlatformSettleRebates(){
//        if (platformSettleRebates == null) {
//            platformSettleRebates = new ArrayList<>();
//        }
//        for (PlatformSettleRebate rebate : platformSettleRebates) {
//            rebate.setPlatformSettle(this);
//        }
//        return platformSettleRebates;
//    }
//
//    @OneToMany(mappedBy = "platformSettle", cascade = {CascadeType.ALL}, orphanRemoval = true)
//    private List<PlatformSettleCost> platformSettleCosts = new ArrayList<>();
//    public List<PlatformSettleCost> getPlatformSettleCosts(){
//        if (platformSettleCosts == null) {
//            platformSettleCosts = new ArrayList<>();
//        }
//        for (PlatformSettleCost cost : platformSettleCosts) {
//            cost.setPlatformSettle(this);
//        }
//        return platformSettleCosts;
//    }
//
//    public void computeAmt(){
//        loanAmt = 0;
//        rebateAmt = 0;
//        payableAmt = 0;
//        for (PlatformSettleItem platformSettleItem : platformSettleItems) {
//            loanAmt += platformSettleItem.getAmount();
//        }
//        for (PlatformSettleRebate platformSettleRebate : platformSettleRebates) {
//            rebateAmt += platformSettleRebate.getActualAmount();
//        }
//        payableAmt = loanAmt - rebateAmt;
//    }


}
